﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Employees_EmployeesInOut : System.Web.UI.Page
{
    private Srvtools.WebDataSet WMaster;



    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitializeComponent(false);

            Master.DataSource = WMaster;

            wgvMaster.Visible = false;


            MasterPage_MasterPage master = (MasterPage_MasterPage)this.Page.Master;
            master.HelpHref = "";
            master.PageName = "员工入职＼离职";
            master.PicUrl = "~/image/PageIcon/shu_emp.jpg";
            master.IntiBar();

            DDlEmpstatus.SelectedValue = "1";
        }
    }

    private void InitializeComponent(bool action)
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Employees_EmployeesInOut));
        this.WMaster = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).BeginInit();
        // 
        // WMaster
        // 
        this.WMaster.Active = action;
        this.WMaster.AlwaysClose = false;
        this.WMaster.PacketRecords = -1;
        this.WMaster.Position = -1;
        this.WMaster.RemoteName = "SEmployees.ViewEmpInOutList";
        this.WMaster.ServerModify = false;
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).EndInit();

    }



    protected void Search_Click(object sender, EventArgs e)
    {
        Search.CssClass = "OnHover";

        string strDep = Depart.SelectDepmanetId;
        if (strDep != "0" && strDep != "")
        {

            strDep = " like ('" + strDep + "%')";

        }
        else
        {
            strDep = " <> -1";
        }


        string sname = SelUser.EmpName;
        if (sname != "")
        {
            sname = "And EmpName like ('%" + sname + "%')";
        }
        else
        {
            sname = "";
        }

        string Status = ddlStatus.SelectedValue;

        string Access = "";
        string Dimis = "";

        if (Status != "-1")
        {
            if (Status == "1")
            {
                Access = Accession.Text != "" ? " And Accession >= '" + Accession.Text + " 00:00:00'" : "";
                Dimis = Dimission.Text != "" ? " And Accession <= '" + Dimission.Text + " 23:59:59'" : "";
            }
            else
            {
                Access = Accession.Text != "" ? " And Dimission >= '" + Accession.Text + " 00:00:00'" : "";
                Dimis = Dimission.Text != "" ? " And Dimission <= '" + Dimission.Text + " 23:59:59'" : "";
            }
        }

        string EmpStatus = DDlEmpstatus.SelectedValue == "-1" ? "" : EmpStatus = " And Employees.EmpStatus=" + DDlEmpstatus.SelectedValue;

        InitializeComponent(true);

        WMaster.SetWhere("Employees.DepartmentId " + strDep + sname + Access + Dimis + EmpStatus);

        object[] ret = Srvtools.CliUtils.CallMethod("SEmployees", "GetEmployeeInOut", new object[] { "Employees.DepartmentId " + strDep + sname + Access + Dimis + EmpStatus });

        string Count = ret[1].ToString();

        LitCurrentCount.Text = "当前人数：" + Count + "人";

        Master.DataSource = WMaster;

        wgvMaster.Visible = true;
    }


    protected void wgvMaster_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;

            Label litSex = (Label)e.Row.FindControl("Sex");
            litSex.Text = drv["Sex"].ToString() == "True" ? "男" : "女";
        }
    }

    protected void DownLoad_Click(object sender, EventArgs e)
    {
        try
        {
            object obj = Master.InnerDataSet;

            string path = MapPath("~/UploadFile/Template/");

            DataTable dt = ((DataSet)obj).Tables[0];


            if (dt.Rows.Count <= 0)
            {
                ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('没有记录')", true);
                return;
            }
            DataTable dts = GetDataTable();

            foreach (DataRow dr in dt.Rows)
            {
                DataRow SR = dts.NewRow();
                SR["员工编号"] = dr["EmployeeID"];
                SR["员工姓名"] = dr["EmpName"];
                SR["性别"] = dr["Sex"].ToString() == "True" ? "男" : "女";
                SR["身份证"] = dr["IDCard"];
                SR["部门"] = dr["DepartmentName"];
                SR["入职时间"] = dr["Accession"];
                SR["离职时间"] = dr["Dimission"]; 
 

                dts.Rows.Add(SR);
            }


            DataTable dtt = dts.Clone();
            dtt.Merge(dts);
            Srvtools.InfoDataSet ids = new Srvtools.InfoDataSet();
            ids.RemoteName = "GLModule.cmdColDEF";
            ids.RealDataSet.Tables.Add(dtt);

            ids.ToExcel(0, path + "empinout.xls");


            System.IO.FileInfo fn = new System.IO.FileInfo(path + "empinout.xls");

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fn.Name);
            HttpContext.Current.Response.WriteFile(fn.FullName);
            HttpContext.Current.Response.End();
        }
        catch
        {

        }
    }

    private DataTable GetDataTable()
    {
        DataTable WTable = new DataTable("DownLoadDT");
        {
            WTable.Columns.Add(new DataColumn("员工编号", typeof(Int32)));
            WTable.Columns.Add(new DataColumn("员工姓名", typeof(string)));
            WTable.Columns.Add(new DataColumn("身份证", typeof(string)));
            WTable.Columns.Add(new DataColumn("性别", typeof(string)));
            WTable.Columns.Add(new DataColumn("部门", typeof(string)));
            WTable.Columns.Add(new DataColumn("入职时间", typeof(string)));
            WTable.Columns.Add(new DataColumn("离职时间", typeof(string)));
        }
        return WTable;
    }
}
